package com.repository;

import com.data_object.TrackDO;
import com.util.DBUtil;
import com.util.Log;
import lombok.SneakyThrows;

import javax.print.attribute.standard.PresentationDirection;
import javax.sound.midi.Track;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author yuisama
 * @date 2023/03/07 21:22
 **/
public class TrackRepo {
    @SneakyThrows
    public static void insert(Integer uid, String title, String type,InputStream content) {
        String sql = "insert into tracks(uid,title,type,content) values(?,?,?,?)";
        try(Connection c = DBUtil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,uid);
                ps.setString(2,title);
                ps.setString(3,type);
                ps.setBinaryStream(4,content);
                //传入二进制流    数据库中的longblob是一种二进制流

                Log.println("执行SQL:"+ ps.toString());
                ps.executeUpdate();
            }
        }
    }

    @SneakyThrows
    public List<TrackDO> selectListByUidLimitOffset(Integer uid,int limit,int offset) {
        //不要一次查太多的content字段，因为content比较大，一次查太多，会导致计算机负载很高
        List<TrackDO> resultList  = new ArrayList<>() ;
        String sql ="select tid,uid,title,type from tracks where uid = ? order by tid desc limit ? offset ?";
        try(Connection c = DBUtil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,uid);
                ps.setInt(2,limit);
                ps.setInt(3,offset);



                System.out.println("执行sql:"+ sql);
                try(ResultSet rs = ps.executeQuery()){
                    while(rs.next()){
                        TrackDO trackDO = new TrackDO(
                                rs.getInt("tid"),
                                rs.getInt("uid"),
                                rs.getString("title"),
                                rs.getString("type")
                        );
                        resultList.add(trackDO);
                    }
                }
            }
        }
        return resultList;
    }


    @SneakyThrows
    public int selectCountByUid(Integer uid) {
        String sql = "select count(*) from tracks where uid = ?";
        try(Connection c  = DBUtil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                ps.setInt(1,uid);

                Log.println("执行SQL:"+sql);
                try(ResultSet rs = ps.executeQuery()){
                    rs.next();  //只有一条
                    return rs.getInt(1);
                }
            }
        }
    }

    @SneakyThrows
    public List<TrackDO> selectListByTidList(List<Integer> tidList) { //[1,2,3,4]
        //List<Integer> -> List<String>   //["1","2","3","4"]
        //String.join("," , List<String>)  // "1,2,3,4"
        String s = String.join(",",tidList.stream().map(String::valueOf).collect(Collectors.toList()));
        //tidList.stream().map(i -> String.valueOf(i));
        //传入一个整形  转为字符串类型
        String sql = String.format("Select tid,uid,title,type from tracks where tid in (%s) order by tid",s);
        //将上个字符串拼接到这里来
        List<TrackDO> list = new ArrayList<>();
        try(Connection c = DBUtil.connection()){
            try(PreparedStatement ps = c.prepareStatement(sql)){
                Log.println("执行SQL:"+sql);

                try(ResultSet rs  = ps.executeQuery()){
                    while(rs.next()){
                        list.add(new TrackDO(
                                rs.getInt("tid"),
                                rs.getInt("uid"),
                                rs.getString("title"),
                                rs.getString("type")
                        ));
                    }
                }
            }
        }
        return list;
    }
}

